/**
 * 
 */
package com.antilia.infomaresme.provider;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.antilia.infomaresme.data.dao.interfaces.IItemsDao;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.Item;

/**
 * @author Ernesto
 *
 */
public abstract class ItemsDataProvider extends SortableDataProvider<Item>  implements ISortableDataProvider<Item> {

	private static final long serialVersionUID = 1L;
	
	private IItemsDao dao;
	
	/**
	 * 
	 */
	public ItemsDataProvider(IItemsDao dao) {
		this.dao = dao;
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.model.IDetachable#detach()
	 */
	public void detach() {

	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<? extends Item> iterator(int first, int count) {
		return dao.getItems(getCategory(), getPlace(), first, count, getSort()).iterator();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		return dao.countItems(getCategory(), getPlace()).intValue();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<Item> model(Item object) {
		return new ItemsLdm(object) {
			
			private static final long serialVersionUID = 1L;

			@Override
			IItemsDao getItemsDao() {
				return dao;
			}
		};
	}

	public abstract Category getCategory();


	public abstract Category getPlace();
}
